<template>
  <div class="app-container">
    <el-form ref="sysuserForm" :model="change" :rules="rules" size="mini">
        <el-form-item label="原密码" prop="password" :label-width="formLabelWidth">
          <el-input v-model="change.password" autocomplete="off" show-password />
        </el-form-item>
        <el-form-item label="新密码" prop="userName" :label-width="formLabelWidth">
          <el-input v-model="change.userName" autocomplete="off" show-password />
        </el-form-item>
        <el-form-item label="确认密码" prop="userName1" :label-width="formLabelWidth">
          <el-input v-model="change.userName1" autocomplete="off" show-password />
        </el-form-item>
     </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" style="float:right;" @click="handleReset()">确 定</el-button>
      </div>
  </div>
</template>

<script>
import { changePassword } from '@/api/User'

const sha256 = require("js-sha256").sha256;

export default {
  data() {
    return {
      doctors: [],
      total: null,
      list: null,
      listLoading: true,
      dialogVisible: false,
      listQuery: {
        page: 1,
        size: 10
      },
      change: {
        password: null,
        userName: null,
        userName1: null,
      },
      sysuser: {
        id: null,
        password: null,
        loginNm: null,
        phoneNo: null,
        weChatNo: null,
        role: null,
        createId: null,
        createTime: null,
        updateId: null,
        updateTime: null,
        delFlag: null,
        userName: null,
        userName1: null,
      },
      sysuserCopy: null,
      rules: {
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }],
        userName: [{ required: true, message: '请填写新密码', trigger: 'blur' }],
        userName1: [{ required: true, message: '请再次填写新密码', trigger: 'blur' }],
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.sysuser.userId = localStorage.getItem('id');
  },
  methods: {
    reset(){
      this.change.password = null;
      this.change.userName = null;
      this.change.userName1 = null;
    },
    handleReset() {
      this.$confirm('是否重置密码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if(this.change.userName!=this.change.userName1){
          this.$message({
            type: 'danger',
            message: '两次输入密码不一致'
          })
          return;
        }
         this.sysuser.password = sha256(this.change.password),
        this.sysuser.userName = sha256(this.change.userName),
        this.sysuser.userName1 = sha256(this.change.userName1),
        changePassword(this.sysuser).then(() => {
          this.$message({
            type: 'success',
            message: '密码修改成功',
            showClose: true,
            duration:0,
          })
          this.reset()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消修改'
        })
      })
    },
  }
}
</script>

<style>
</style>
